package com.amigo.online.provider.adv.service.impl;

import java.util.Date;
import java.util.List;

import com.amigo.online.provider.adv.mapper.AdvHomeApplyAndAreaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.common.param.adv.bo.AdvHomeInitBO;
import com.amigo.online.common.param.result.AdvInfo;
import com.amigo.online.provider.adv.common.ConstantValue;
import com.amigo.online.provider.adv.mapper.AdvHomeApplyMapper;
import com.amigo.online.provider.adv.service.AdvHomeApplyService;
import com.amigo.online.provider.adv.service.AdvHomeSourceService;
import com.amigo.online.provider.adv.service.common.AbstractAdvApplyService;
import com.amigo.online.provider.adv.util.AdvBasicUtil;

@Service
public class AdvHomeApplyServiceImpl extends AbstractAdvApplyService implements AdvHomeApplyService {

    @SuppressWarnings("rawtypes")
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    AdvHomeSourceService advHomeSourceService;

    @Autowired
    AdvHomeApplyMapper advHomeApplyMapper;
    @Autowired
    AdvHomeApplyAndAreaMapper advHomeApplyAndAreaMapper;

    @Override
    public List<AdvHomeInitBO> listFindTomorrowAdvSettlePlanByEndTime(long today) {
        return advHomeApplyMapper.listFindTomorrowAdvSettlePlanByEndTime(today);
    }

    @Override
    public List<AdvHomeInitBO> listFindTomorrowAdvDeliveryPlanByStartTime(long tomorrow) {
        return advHomeApplyMapper.listFindTomorrowAdvDeliveryPlanByStartTime(tomorrow);
    }

    /**
     * 获取投放的广告
     * @param sexType
     * @param ageType
     * @param lng
     * @param lat
     * @param areaId
     * @param number
     * @param ratio
     * @return
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<AdvInfo> listHomeAdvByAreaIdAndSexTypeAndAgeType(Integer sexType, Integer ageType,
                                                                 Double lng, Double lat, int number, double ratio) {
        return advHomeApplyAndAreaMapper.selectByAreaId(new Date().getTime()/1000);
    }
//    /**
//     * 获取投放的广告
//     * @param sexType
//     * @param ageType
//     * @param lng
//     * @param lat
//     * @param areaId
//     * @param number
//     * @param ratio
//     * @return
//     */
//    @SuppressWarnings("unchecked")
//    @Override
//    public List<AdvInfo> listHomeAdvByAreaIdAndSexTypeAndAgeType(Integer sexType, Integer ageType,
//                                                                 Double lng, Double lat, Integer areaId, int number, double ratio) {
//        List<AdvInfo> result = new ArrayList<>();
//        areaId= 8934;
//        // 区域
//        String redisKey = AdvBasicUtil.spliceAdvRedisKey(ConstantValue.ADV_HOME_LIST, areaId, sexType, ageType);
//        // 获取当前城市广告
//        try {
//            initHomeAdvStockJob.execute(null);
//        } catch (JobExecutionException e) {
//            e.printStackTrace();
//        }
//        List<Integer> areaAdvList = redisTemplate.opsForList().range(redisKey, 0, -1);
//        Set<Integer> advListSet = new HashSet<>();
//        if (areaAdvList.size() < number && areaId != 0) {
//            // 区域的不够 ，全国补充
//            String globalRedisKey = AdvBasicUtil.spliceAdvRedisKey(ConstantValue.ADV_HOME_LIST, 0, sexType,
//                    ageType);
//            areaAdvList.addAll(redisTemplate.opsForList().range(globalRedisKey, 0, -1));
//        }
//        advListSet.addAll(areaAdvList);
//        List<Integer> typeList = redisTemplate.opsForHash().multiGet(ConstantValue.ADV_HOME_TYPE_MAP, advListSet);
//        Map<Integer, Set<Integer>> mapList = new HashMap<>();
//        Iterator<Integer> it = advListSet.iterator();
//        int index = 0;
//        while (it.hasNext()) {
//            Set<Integer> temp = mapList.get(typeList.get(index));
//            if (temp == null) {
//                temp = new HashSet<>(32);
//                temp.add(it.next());
//                mapList.put(typeList.get(index), temp);
//            } else {
//                temp.add(it.next());
//            }
//            index++;
//        }
//        Set<Integer> advSet = new HashSet<>();
//        long random = Math.round(Math.random());
//        int tryTimes = 0;
//        while (advSet.size() == 0 && tryTimes < 2) {
//            if (random == 0) {
//                if (mapList.get(1) != null && mapList.get(1).size() >= 1) {
//                    List<Integer> threeRemainingList = redisTemplate.opsForHash().multiGet(ConstantValue.ADV_HOME_STOCK, mapList.get(1));
//                    advSet = getRandomAdvIds(mapList.get(1), threeRemainingList, 1);
//                    if (advSet.size() != 1) {
//                        advSet = null;
//                    }
//                }
//                random = 1;
//            } else if (random == 1) {
//                if (mapList.get(3) != null && mapList.get(3).size() >= 3) {
//                    List<Integer> threeRemainingList = redisTemplate.opsForHash().multiGet(ConstantValue.ADV_HOME_STOCK, mapList.get(3));
//                    advSet = getRandomAdvIds(mapList.get(3), threeRemainingList, 3);
//                    if (advSet.size() != 3) {
//                        advSet = null;
//                    }
//                }
//                random = 0;
//            }
//			/*else if (random == 2 && twoflag == true) {
//			if(mapList.get(2)!= null && mapList.get(2).size() >= 2) {
//				List<Integer> threeRemainingList = redisTemplate.opsForHash().multiGet(ConstantValue.ADV_HOME_STOCK, mapList.get(2));
//				advSet = getRandomAdvIds(mapList.get(2), threeRemainingList, 2);
//				if(advSet.size() != 2) {
//					advSet = null;
//				}
//				twoflag = false;
//			}
//		    }*/
//            tryTimes++;
//        }
//
//        if (advSet.size() != 0) {
//            result = advHomeSourceService.getAdvSourceBySizeAndIds(ratio, advSet);
//        }
//
//        // 获取当前点附近广告
//        // List<Long> nearbyList = getAdvByNearBy(lng, lat, number, sexType, ageType);
//
//        // 可用广告组合
//        // List<Long> advList = complexList(areaList,nearbyList);
//
//        // 看用户看过的广告列表
//        // List<Long> userLooked =
//        // redisTemplate.opsForList().range(ConstantValue.ADV_USER_LOOKED_LIST + userId,
//        // 0, -1);
//
//        // 去掉用户当天看过的广告
//        // advList.removeAll(userLooked);
//
//        // 获取剩余广告对应的剩余播放量
//
//        /*
//         * if(advSet.size() != 0) { //计算比率 double ratio = width / height; result =
//         * advHomeSourceService.getAdvSourceBySizeAndIds(ratio, advSet); }
//         */
//        // 获取广告结果
//        return result;
//    }

}
